package controller;

import java.io.IOException;
import java.io.PrintWriter;
import java.text.SimpleDateFormat;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import model.DAO;
import model.EventInfo;
import model.EventPlus;
import model.UserProfile;

import org.codehaus.jackson.map.JsonMappingException;
import org.codehaus.jackson.map.ObjectMapper;
import org.codehaus.jackson.*;

public class EventServlet extends HttpServlet{

	private DAO dao = new DAO();
	ObjectMapper mapper = new ObjectMapper();

	private String getEventID(long eid) throws JsonGenerationException, JsonMappingException, IOException
	{				
		Map<String,String> nameStruct = new HashMap<String,String>();
		nameStruct.put("eid", String.valueOf(eid));
	
		return mapper.writeValueAsString(nameStruct);		
	}
	
	private String getNearEvents(String uid, double range) throws JsonGenerationException, JsonMappingException, IOException
	{
		List<EventInfo> list = dao.getNearEventList(uid, range, false);
		return mapper.writeValueAsString(list);
	}
	
	private String getMyCreateEvents(String uid) throws JsonGenerationException, JsonMappingException, IOException
	{
		List<EventInfo> list = dao.getMyCreateEventList(uid);
		return mapper.writeValueAsString(list);
	}
	
	private String getMyPlusEvents(String uid) throws JsonGenerationException, JsonMappingException, IOException
	{
		List<EventInfo> list = dao.getMyPlusEventList(uid);
		return mapper.writeValueAsString(list);
	}
	
	private String getPastEvents(String uid, double range) throws JsonGenerationException, JsonMappingException, IOException
	{
		List<EventInfo> list = dao.getNearEventList(uid, range, true);
		return mapper.writeValueAsString(list);
	}

	private String getEventPhoto(long id) 
	{
		EventInfo user = dao.getEventInfoById(id);		
		return user.getPhoto().getKeyString();		
	}
	
	private String getEventInfo(long id) throws JsonGenerationException, JsonMappingException, IOException
	{
		EventInfo user = dao.getEventInfoById(id);		
		return mapper.writeValueAsString(user);		
	}
	
	private String getPlusUser(long eid) throws JsonGenerationException, JsonMappingException, IOException
	{
		List<UserProfile> list = dao.getPlusUser(eid);
		return mapper.writeValueAsString(list);
	}		
	
	private void updateLoc(long id, double x, double y)
	{
		EventInfo event = dao.getEventInfoById(id);
		event.getStatus().setX(x);
		event.getStatus().setY(y);
		dao.ofy().put(event);	
	}
	
	private void plusEvent(long eid, String uid) 
	{
		EventPlus ep = dao.createEventPlus(eid, uid);
		dao.ofy().put(ep);
	}
	
	@Override
	protected void doGet(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {
			
		long eid = 0;
		if(req.getParameter("eid") != null)
			eid = Long.parseLong(req.getParameter("eid"));
		
		String action = req.getParameter("action");
		String result = "No Action!!";
				
		if(action!=null){
			if(action.equals("scan")) {
				double range = 20.0;
				String uid = req.getParameter("uid");
				
				if(req.getParameter("range")!=null)
					range = Double.parseDouble(req.getParameter("range"));
				String type = req.getParameter("type");
				if(type.equals("near"))
					result = getNearEvents(uid, range);
				else if(type.equals("create"))
					result = getMyCreateEvents(uid);
				else if(type.equals("plus"))
					result = getMyPlusEvents(uid);
				else if(type.equals("past"))
					result = getPastEvents(uid, range);
			}
			
			if(eid!=0){
				if(action.equals("create")) {
					result = getEventID(eid);
				} else if(action.equals("view")) {
					result = getEventInfo(eid);
				} else if(action.equals("listplususer")) {
					result = getPlusUser(eid);
				} else if(action.equals("getphoto")) {
					String photoKey = getEventPhoto(eid);
					resp.sendRedirect("/serve?blob-key=" + photoKey);
				}
			}
		}
		
		resp.setContentType("application/json;charset=UTF-8");
        PrintWriter out = resp.getWriter();        
		out.println(result);
	}
	
	@Override
	protected void doPost(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {
				
		//String result = "No Action!!";
		String action = req.getParameter("action");
		long id = 0; 
		if(req.getParameter("id")!=null)
			id = Long.parseLong(req.getParameter("id"));
		
		if(action.equals("location")) {
			double x = Double.parseDouble(req.getParameter("x"));
			double y = Double.parseDouble(req.getParameter("y"));
			updateLoc(id, x, y);
		}			
		else if(action.equals("plus")) {
			long eid = Long.parseLong(req.getParameter("eid"));
			String uid = req.getParameter("uid");
			plusEvent(eid, uid);
		}
		else if(action.equals("photo")) {
			long eid = Long.parseLong(req.getParameter("eid"));
			String uid = req.getParameter("uid");
			plusEvent(eid, uid);
		}
	
		//resp.setContentType("text/plain");
		//resp.getOutputStream().write("OK!".getBytes());
	}

}
